package com.freud.logic.business.service;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.freud.framework.base.BaseService;
import com.freud.framework.util.DateUtil;
import com.freud.logic.business.bean.Car;
import com.freud.logic.business.bean.Good;
import com.freud.logic.business.mapper.CarMapper;
import com.freud.logic.business.mapper.GoodMapper;

@Service
@Transactional
public class CarService extends BaseService {

	@Autowired
	private CarMapper carMapper;

	@Autowired
	private GoodMapper goodMapper;

	public List<Car> getAll(RowBounds rb) {
		final List<Car> cars = carMapper.getAll(rb);
		for (final Car car : cars) {
			this.convert(car);
		}
		return cars;
	}

	public Car getById(int id) {
		return this.convert(carMapper.get(id));
	}

	public void insert(Car car) {
		car.setStatus(false);
		car.setGoodId(0);
		car.setOperateDate(DateUtil.currentTimestamp());
		carMapper.insert(car);
	}

	public void update(Car car) {
		Car exist = carMapper.get(car.getId());
		exist.setCarNo(car.getCarNo());
		exist.setPlatNo(car.getPlatNo());
		exist.setLoad(car.getLoad());
		exist.setOperateDate(DateUtil.currentTimestamp());
		carMapper.update(exist);
	}

	public void sendCar(Car car) {
		Car exist = carMapper.get(car.getId());
		exist.setStatus(true);
		exist.setGoodId(car.getGoodId());
		exist.setOperateDate(DateUtil.currentTimestamp());
		carMapper.update(exist);
	}

	public void backCar(int id) {
		Car exist = carMapper.get(id);
		exist.setStatus(false);
		exist.setGoodId(0);
		exist.setOperateDate(DateUtil.currentTimestamp());
		carMapper.update(exist);
	}

	public void delete(Integer id) {
		carMapper.delete(id);
	}

	public Car convert(final Car car) {
		Good good = goodMapper.get(car.getGoodId());
		car.setGood(good == null ? new Good() : good);
		return car;
	}
}
